package com.vegetable.modules.service.outbound.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.outbound.SalesOrderDetailStock;
import com.vegetable.modules.mapper.outbound.SalesOrderDetailStockMapper;
import com.vegetable.modules.service.outbound.ISalesOrderDetailStockService;
import com.vegetable.modules.service.stock.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 销售订单走货对照库存表 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-29
 */
@Slf4j
@Service
public class SalesOrderDetailStockServiceImpl extends ServiceImpl<SalesOrderDetailStockMapper, SalesOrderDetailStock> implements ISalesOrderDetailStockService {
    @Resource
    private IStockService iStockService;
    @Override
    public List<SalesOrderDetailStock> selectByDetailId(Long detailId) {
//        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("detail_id", detailId);
//        queryWrapper.and(wrapper -> wrapper.gt("number", 0).or().gt("weight", BigDecimal.ZERO));
        return this.baseMapper.selectSalesOrderDetailStock(detailId, null, null);
    }

    @Override
    public List<SalesOrderDetailStock> selectByDetailId2(Long detailId) {
        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("detail_id", detailId);
        queryWrapper.and(wrapper -> wrapper.gt("number", 0).or().gt("weight", BigDecimal.ZERO));
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<SalesOrderDetailStock> selectByOrderId(Long orderId) {
        return this.baseMapper.selectSalesOrderDetailStock2(orderId);
    }

    @Override
    public List<SalesOrderDetailStock> selectByDetailIds(List<Long> detailIds) {
//        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("detail_id", detailIds);
//        queryWrapper.and(wrapper -> wrapper.gt("number", 0).or().gt("weight", BigDecimal.ZERO));
        return this.baseMapper.selectSalesOrderDetailStock(null, null, detailIds);
    }

    @Override
    public List<SalesOrderDetailStock> selectByStockId(Long stockId) {
//        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("stock_id", stockId);
        List<SalesOrderDetailStock> detailStocks = this.baseMapper.selectSalesOrderDetailStock(null, stockId, null);
        return  detailStocks;
    }

    @Override
    public Integer deleteByStockId(Long stockId) {
        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stock_id", stockId);
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public Integer deleteByDetailId(Long detailId) throws Exception {
        //
        List<SalesOrderDetailStock> detailStocks = selectByDetailId(detailId);
        if(CollectionUtil.isNotEmpty(detailStocks)){
            for(SalesOrderDetailStock ds : detailStocks){
                iStockService.releaseStockByDetailId(ds.getDetailId());
            }
        }
        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("detail_id", detailId);
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public Integer deleteById(Long id) {

        return baseMapper.deleteById(id);
    }

    @Override
    public List<SalesOrderDetailStock> selectList(Long orderId, Long detailId, Integer orderType) {
        QueryWrapper<SalesOrderDetailStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("detail_id", detailId);
        queryWrapper.eq("type", orderType);
        queryWrapper.and(wrapper -> wrapper.gt("number", 0).or().gt("weight", BigDecimal.ZERO));
        return this.baseMapper.selectList(queryWrapper);
    }
}
